<?php

namespace app\models;

use Yii;

class Branch extends \yii\db\ActiveRecord
{
    public function extraFields()
    {
        $fields['department'] = function ($model){
            return $model->department;
        };
        return $fields;
    }

    public function getDepartment(){
        return $this->hasMany(Department::class,['branch_id'=>'id']);
    }

    public static function getOrSet() {
        return Yii::$app->cache->getOrSet('branch', function() {
            return self::find()->asArray()->all();
        });
    }

    public static function getBranchName($id) {
        $branch = self::getOrSet();
        foreach ($branch as $k => $v) {
            if ($v['id'] == $id) {
                return $branch;
            }
        }
    }
}
